

#ifndef WIRELESS_DRIVER_H
#define WIRELESS_DRIVER_H

#include "SPI_Simulator.h"

#define RADIO_XTAL_FREQUENCY 30000000L
#define RADIO_MODULE_FREQUENCY 433 //MHz
#define RADIO_MODULE_STEP_SIZE 250//kHz
#define RADIO_TX_DATA_RATE 100000//10k->10X


#define WIRELESS_IRQ_PORT GPIOF
#define WIRELESS_IRQ_PIN GPIO_Pin_0

#define WIRELESS_SDN_PORT GPIOD
#define WIRELESS_SDN_PIN GPIO_Pin_7


#define CLEAR_FIFO_TX 0X01
#define CLEAR_FIFO_RX 0X02


#define WIRELESS_DATA_RATE_1K 0
#define WIRELESS_DATA_RATE_2K 1
#define WIRELESS_DATA_RATE_5K 2
#define WIRELESS_DATA_RATE_10K 3
#define WIRELESS_DATA_RATE_20K 4
#define WIRELESS_DATA_RATE_40K 5

#define REGISTER_DISABLE 0
#define REGISTER_LATCHED_RSSI 10

#define U8 unsigned char 






typedef enum
{
	NOP = 0X00,
	PART_INFO = 0X01,
	POWER_UP = 0X02,
	FUNC_INFO = 0X10,
	SET_PROPERTY,
	GET_PROPERTY,
	GPIO_PIN_CFG,
	GET_ADC_READING,
	FIFO_INFO,
	PACKET_INFO,
	IRCAL,
	PROTOCOL_CFG,
	GET_INT_STATUS = 0X20,
	GET_PH_STATUS,
	GET_MODEM_STATUS,
	GET_CHIP_STATUS,
	START_TX = 0X31,
	START_RX,
	REQUEST_DEVICE_STATE,
	CHANGE_STATE,
	START_MFSK,
	RX_HOP,
	READ_CMD_BUFF =0X44,
	FRR_A_READ = 0X50,
	FRR_B_READ = 0X51,
	FRR_C_READ = 0X53,
	FRR_D_READ = 0X57,
	WRITE_TX_FIFO = 0X66,
	READ_RX_FIFO = 0X77,
	
	COMMAND_MAX
	
}EnumCommandTable;

typedef enum
{

	GLOBAL_XO_TUNE = 0x0000,
	GLOBAL_CLK_CFG,
	GLOBAL_LOW_BATT_THRESH,
	GLOBAL_CONFIG ,
	GLOBAL_WUT_CONFIG ,
	GLOBAL_WUT_M_15_8 ,
	GLOBAL_WUT_M_7_0 ,
	GLOBAL_WUT_R ,
	GLOBAL_WUT_LDC ,
//0X0100
	INT_CTL_ENABLE = 0x0100,
	INT_CTL_PH_ENABLE ,
	INT_CTL_MODEM_ENABLE ,
	INT_CTL_CHIP_ENABLE ,
	FRR_CTL_A_MODE = 0x0200,
	FRR_CTL_B_MODE ,
	FRR_CTL_C_MODE ,
	FRR_CTL_D_MODE ,
//0X1000
	PREAMBLE_TX_LENGTH = 0x1000,
	PREAMBLE_CONFIG_STD_1 ,
	PREAMBLE_CONFIG_NSTD ,
	PREAMBLE_CONFIG_STD_2 ,
	PREAMBLE_CONFIG ,
	PREAMBLE_PATTERN_31_24 ,
	PREAMBLE_PATTERN_23_16 ,
	PREAMBLE_PATTERN_15_8 ,
	PREAMBLE_PATTERN_7_0 ,
//0X1100
	SYNC_CONFIG = 0x1100,
	SYNC_BITS_31_24 ,
	SYNC_BITS_23_16 ,
	SYNC_BITS_15_8 ,
	SYNC_BITS_7_0 ,
//0x1200
	PKT_CRC_CONFIG = 0x1200,
	PKT_WHT_POLY_15_8,
	PKT_WHT_POLY_7_0,
	PKT_WHT_SEED_15_8,
	PKT_WHT_SEED_7_0,
	PKT_WHT_BIT_NUM,
	PKT_CONFIG1,
	PKT_RESERVE,
	PKT_LEN ,
	PKT_LEN_FIELD_SOURCE,
	PKT_LEN_ADJUST ,
	PKT_TX_THRESHOLD ,
	PKT_RX_THRESHOLD ,
	PKT_FIELD_1_LENGTH_12_8 ,
	PKT_FIELD_1_LENGTH_7_0 ,
	PKT_FIELD_1_CONFIG ,
	PKT_FIELD_1_CRC_CONFIG,
	PKT_FIELD_2_LENGTH_15_8,
	PKT_FIELD_2_LENGTH_7_0,
	PKT_FIELD_2_CONFIG,
	PKT_FIELD_2_CRC_CONFIG,
	PKT_FIELD_3_LENGTH_15_8,
	PKT_FIELD_3_LENGTH_7_0,
	PKT_FIELD_3_CONFIG,
	PKT_FIELD_3_CRC_CONFIG,
	PKT_FIELD_4_LENGTH_15_8,
	PKT_FIELD_4_LENGTH_7_0,
	PKT_FIELD_4_CONFIG,
	PKT_FIELD_4_CRC_CONFIG,
	PKT_RX_FIELD_1_LENGTH_15_8,
	PKT_RX_FIELD_1_LENGTH_7_0,
	PKT_RX_FIELD_1_CONFIG,
	PKT_RX_FIELD_1_CRC_CONFIG,
	PKT_RX_FIELD_2_LENGTH_15_8,
	PKT_RX_FIELD_2_LENGTH_7_0,
	PKT_RX_FIELD_2_CONFIG,
	PKT_RX_FIELD_2_CRC_CONFIG,
//0x2000
	MODEM_MOD_TYPE = 0x2000,
	MODEM_MAP_CONTROL,
	MODEM_DSM_CTRL,
	MODEM_DATA_RATE,
	MODEM_TX_NCO_MODE = 0x2006,
	MODEM_FREQ_DEV = 0x200a,
	MODEM_FREQ_OFFSET = 0x200d,
	MODEM_TX_FILTER_COEFF_8,
	MODEM_TX_RAMP_DELAY = 0x2018,
	MODEM_MDM_CTRL,
	MODEM_IF_CONTROL,
	MODEM_IF_FREQ,
	MODEM_DECIMATION_CFG1 = 0x201e,
	MODEM_DECIMATION_CFG0,
	MODEM_BCR_OSR,
	MODEM_BCR_NCO_OFFSET = 0x2024,
	MODEM_BCR_GAIN = 0x2027,
	MODEM_BCR_GEAR = 0x2029,
	MODEM_BCR_MISC1,
	MODEM_BCR_MISC0,
	MODEM_AFC_GEAR,
	MODEM_AFC_WAIT,
	MODEM_AFC_GAIN,
	MODEM_AFC_LIMITER = 0x2030,
	MODEM_AFC_MISC = 0x2032,
	MODEM_AFC_ZIFOFF,
	MODEM_ADC_CTRL,
	MODEM_AGC_CONTROL,
	MODEM_AGC_WINDOW_SIZE,
	MODEM_AGC_RFPD_DECAY,
	MODEM_AGC_IFPD_DECAY,
	MODEM_FSK4_GAIN1,
	MODEM_FSK4_GAIN0,
	MODEM_FSK4_TH,
	MODEM_FSK4_MAP = 0x203f,
	MODEM_OOK_PDTC,
	MODEM_OOK_BLOPK,
	MODEM_OOK_CNT1,
	MODEM_OOK_MISC,
	MODEM_RAW_SEARCH,
	MODEM_RAW_CONTROL,
	MODEM_RAW_EYE,
	MODEM_ANT_DIV_MODE = 0x2048,
	MODEM_ANT_DIV_CONTROL,
	MODEM_RSSI_THRESH,
	MODEM_RSSI_JUMP_THRESH,
	MODEM_RSSI_CONTROL,
	MODEM_RSSI_CONTROL2,
	MODEM_RSSI_COMP,
	MODEM_CLKGEN_BAND= 0x2051,
//0x2200
	PA_MODE = 0x2200,
	PA_PWR_LVL,
	PA_BIAS_CLKDUTY,
	PA_TC,
	PA_RAMP_EX,
	PA_RAMP_DOWN_DELAY,

//0x2300
	SYNTH_PFDCP_CPFF = 0x2300,
	SYNTH_PFDCP_CPINT,
	SYNTH_VCO_KV,
	SYNTH_LPFILT3 = 0x2303,
	SYNTH_LPFILT2,
	SYNTH_LPFILT1,
	SYNTH_LPFILT0,
	SYNTH_VCO_KVCAL,
//0x3000
	MATCH_VALUE_1=0x3000,
	MATCH_MASK_1,
	MATCH_CTRL_1,
	MATCH_VALUE_2,
	MATCH_MASK_2,
	MATCH_CTRL_2,
	MATCH_VALUE_3,
	MATCH_MASK_3,
	MATCH_CTRL_3,
	MATCH_VALUE_4,
	MATCH_MASK_4,
	MATCH_CTRL_4,

//0x4000
	FREQ_CONTROL_INTE = 0x4000,
	FREQ_CONTROL_FRAC,
	FREQ_CONTROL_CHANNEL_STEP_SIZE = 0x4004,
	FREQ_CONTROL_W_SIZE = 0x4006,
	FREQ_CONTROL_VCOCNT_RX_ADJ,
//0x5000
	RX_HOP_CONTROL = 0x5000,
	RX_HOP_TABLE_SIZE,
	RX_HOP_TABLE_ENTRY
}EnumPropertyTable;

#define CMD_PARA_LEN 30
#define RELAY_STREAM_LEN 30
typedef struct
{
	unsigned char CmdPara[CMD_PARA_LEN];
	unsigned char CmdParaRealLen;
	unsigned char RelyStream[RELAY_STREAM_LEN];
	unsigned char RelyStreamRealLen;
}StructRadioCmdStream;

extern StructRadioCmdStream gsRadioCmdStream;


#define STATE_NO_CHANGE 0
#define STATE_SLEEP 1
#define STATE_SPI_ACTIVE 2
#define STATE_READY 3
#define STATE_ANOTHER_READY 4
#define STATE_TUNE_TX 5
#define STATE_TUNE_RX 6
#define STATE_TX 7
#define STATE_RX 8

void WirelessSleep(void);
void WirelessWakeup(void);

void WirelessTestSPI(void);
void WirelessSendCommand(StructRadioCmdStream *rcs);

void WirelessInitPara(void);

void WirelessRequestDeviceState(void);
void WirelessSynthesizerFrequencyControl(void);


void WirelessStartTX(unsigned char channel,unsigned char condition,unsigned short int len);
void WirelessStartRX(unsigned char channel,unsigned char condition,unsigned short int len);
void WirelessWriteDataToFIFO(const unsigned char *dat,unsigned char len);
void WirelessReadDataFromFIFO(unsigned char *dat,unsigned char len);
void WirelessSetInterruptProperty(void);
void WirelessChangeState(unsigned char stt);
void WirelessSetSendLength(unsigned char len);

void WirelessGetInterruptStatus(void);
void WirelessGetFifoInfo(unsigned char clr);
void WirelessGetInterruptProperty(void);

void WirelessPowerUp(void);

void WirelessConfigCommand(unsigned char cmd,
                                                         const unsigned char *InPara,
                                                         unsigned char len);
void WirelessGetCommandStatus(unsigned char cmd,
                                                               const unsigned char *InPara,
                                                               unsigned char Inlen,
                                                               unsigned char OutLen );
                                                            
void WirelessConfigProperty(unsigned char GroupID,
                                                      unsigned char IDNums,
                                                      unsigned char StartID,
                                                      const unsigned char *Inpara);


void WirelessGetProperty(unsigned char GroupID,unsigned char IDNums,unsigned char StartID);
void WirelessGetTXDataRate(void);

void WirelessConfigGPIO(void);

void WirelessSetTXDataRate(void);

void rf_init_new(void);

void WirelessSetSyncword(unsigned short int syncword);

void WirelessCloseInterruptProperty(void);

unsigned char WirelessGetFastResponseRegister(unsigned char bStartReg, 
												       unsigned char bNmbrOfRegs, 
												       unsigned char * pbRegValues);
void WirelessConfigFastRegisters(unsigned char RegA,
                                                               unsigned char RegB,
                                                               unsigned char RegC,
                                                               unsigned char RegD);

                                                           

void rf_change_TRrate( unsigned char param);

void WirelessConfigCRC(void);

void SI4463EnterSleep(void);
                                              
#endif






